package kotan.datastore.api;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Entity implements Serializable {

    private static final long serialVersionUID = 1L;
    final Key key;
    final Map<String, Object> propertyMap = new HashMap<String, Object>();

    public Entity(Key key) {
        this.key = key;
    }

    public Key getKey() {
        return key;
    }

    public Object getProperty(String key) {
        return propertyMap.get(key);
    }

    public void setProperty(String key, Object value) {
        propertyMap.put(key, value);
    }
    
    public Map<String, Object> getProperties() {
        return propertyMap;
    }

    public void setPropertiesFrom(Entity entity) {
        this.propertyMap.putAll(entity.propertyMap);
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        result = prime * result + ((propertyMap == null) ? 0 : propertyMap.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Entity other = (Entity) obj;
        if (key == null) {
            if (other.key != null) return false;
        } else if (!key.equals(other.key)) return false;
        if (propertyMap == null) {
            if (other.propertyMap != null) return false;
        } else if (!propertyMap.equals(other.propertyMap)) return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("Entity");
        str.append("key=").append(key);
        str.append(", propertyMap=").append(propertyMap);
        str.append("]");
        return str.toString();
    }
}
